package de.bassistance.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateGenerator {
	
	private DateFormat dayFormat;
	private DateFormat abbrDayFormat;
	private DateFormat monthFormat;
	private DateFormat abbrMonthFormat;
	
	private Locale locale;
	
	DateGenerator(Locale locale) {
		this.locale = locale;
		dayFormat = new SimpleDateFormat("EEEE", locale);
		abbrDayFormat = new SimpleDateFormat("EEE", locale);
		monthFormat = new SimpleDateFormat("MMMM", locale);
		abbrMonthFormat = new SimpleDateFormat("MMM", locale);
	}
	
	public String all() {
		StringBuilder result = new StringBuilder();
		result.append("// date localization for locale '");
		result.append(locale);
		result.append("'\n");
		result.append("// generated by Jörn Zaefferer using Java's java.util.SimpleDateFormat\n");
		result.append(dayNames());
		result.append("\n");
		result.append(abbrDayNames());
		result.append("\n");
		result.append(monthNames());
		result.append("\n");
		result.append(abbrMonthNames());
		result.append("\n");
		return result.toString();
	}
	
	public String dayNames() {
		List<String> names = new ArrayList<String>();
		for (int i = 0; i < 7; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY + i);
			names.add("'" + day(calendar.getTime()) + "'");
		}
		return "Date.dayNames = " + names.toString() + ";";
	}
	
	public String abbrDayNames() {
		List<String> names = new ArrayList<String>();
		for (int i = 0; i < 7; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY + i);
			names.add("'" + abbrDay(calendar.getTime()) + "'");
		}
		return "Date.abbrDayNames = " + names.toString() + ";";
	}
	
	public String monthNames() {
		List<String> names = new ArrayList<String>();
		for (int i = 0; i < 12; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.MONTH, Calendar.JANUARY + i);
			names.add("'" + month(calendar.getTime()) + "'");
		}
		return "Date.monthNames = " + names.toString() + ";";
	}
	
	public String abbrMonthNames() {
		List<String> names = new ArrayList<String>();
		for (int i = 0; i < 12; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.MONTH, Calendar.JANUARY + i);
			names.add("'" + abbrMonth(calendar.getTime()) + "'");
		}
		return "Date.abbrMonthNames = " + names.toString() + ";";
	}
	
	private String day(Date date) {
		return dayFormat.format(date);
	}
	
	private String abbrDay(Date date) {
		return abbrDayFormat.format(date);
	}
	
	private String month(Date date) {
		return monthFormat.format(date);
	}
	
	private String abbrMonth(Date date) {
		return abbrMonthFormat.format(date);
	}

}
